import 'package:dk_flutter_module_sale/thirdParty/flutter_calendar_carousel.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:dk_flutter_base/dk_ui.dart';
import 'package:flutter_dk_form/flutter_dk_form.dart';

class PageTimeChooseDemoWidget extends StatefulWidget {
  static const String routeName = '/page/time_choose_demo_page';
  static const String name = "TimeChoose Demo";
  final btnData = ['日期选择', '时间选择', '时间范围选择'];

  @override
  State<StatefulWidget> createState() {
    return PageTimeChooseDemoWidgetState();
  }
}

class PageTimeChooseDemoWidgetState extends State<PageTimeChooseDemoWidget> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("TimeChoose Demo")),
      body: Column(
        children: <Widget>[
          ListView.builder(
              itemCount: widget.btnData.length,
              shrinkWrap: true,
              itemBuilder: (context, index) {
                return InkWell(
                  onTap: () {
                    if (index == 0) {
                      showPickerDate(context);
                    } else if (index == 1) {
                      showPickerTime(context);
                    } else if (index == 2) {
                      CalendarCarousel();
                    }
                  },
                  child: new Card(
                    child: new Container(
                      alignment: Alignment.centerLeft,
                      margin: EdgeInsets.symmetric(horizontal: 10),
                      height: 50,
                      child: new Text(widget.btnData[index]),
                    ),
                  ),
                );
              }),
          timeChoose(),
        ],
      ),
    );
  }

  DateTime _currentSelectDate;
  DateTime _lastSelectDate;

  Widget timeChoose() {
    return Container(
        margin: EdgeInsets.symmetric(horizontal: 16.0),
        child: Container(
          height: 400,
          child: CalendarCarousel(
            onDayPressed: (DateTime date) {
              this.setState(() => _currentSelectDate = date);
            },
            weekendTextStyle: TextStyle(
              color: Colors.red,
            ),
            height: 400.0,
            selectedDateTime: _currentSelectDate,
            lastSelectedDateTime: _lastSelectDate,
            minSelectedDate: DateTime.now().subtract(Duration(days: 365 * 2)),
            maxSelectedDate: DateTime.now().add(Duration(days: 365 * 2)),
            selectedDayButtonColor:Color(0xff3DBCC6),
            onCalendarChanged: (DateTime startDate, DateTime endDate) {
              setState(() {});
            },
            onCalendarPageChanged: (DateTime date) {
              setState(() {});
            },
            onCalendarConfirm: (String startDate, String endDate) {
              showDkToast("开始时间：" + startDate + "-结束时间：" + endDate);
              setState(() {});
            },
            onResetCalendarChanged: () {
              setState(() {
                _currentSelectDate = null;
                _lastSelectDate = null;
              });
            },
          ),
        ));
  }

  showPickerDate(BuildContext context) {
    new DkPicker(
      adapter: new DateTimePickerAdapter(
          type: PickerDateTimeType.kYMD,
          isNumberMonth: true,
          yearSuffix: "年",
          monthSuffix: "月",
          daySuffix: "日"),
      title: new Text(
        "选择签约日期",
        style: TextStyle(color: Color(0xFF999999), fontSize: 14),
      ),
      onConfirm: (DkPicker picker, List value) {
        print(picker.adapter.text);
      },
      onSelect: (DkPicker picker, int index, List<int> selecteds) {
        this.setState(() {
//            stateText = picker.adapter.toString();
        });
      },
      cancelTextStyle: TextStyle(color: Color(0xFF666666), fontSize: 14),
    ).showModal(context);
  }

  showPickerTime(BuildContext context) {
    new DkPicker(
      adapter: new DateTimePickerAdapter(
        type: PickerDateTimeType.kHM,
      ),
      title: new Text(
        "选择时间",
        style: TextStyle(color: Color(0xFF999999), fontSize: 14),
      ),
      onConfirm: (DkPicker picker, List value) {
        print(picker.adapter.text);
      },
      onSelect: (DkPicker picker, int index, List<int> selecteds) {
        this.setState(() {
//            stateText = picker.adapter.toString();
        });
      },
      cancelTextStyle: TextStyle(color: Color(0xFF666666), fontSize: 14),
    ).showModal(context);
  }
}
